package ui;

import io.GPSPoint;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.NumberFormatter;

import math.Distance;
import actions.Settings;

@SuppressWarnings("serial")
public class StatsPanel extends JPanel implements PropertyChangeListener {

	private Map<String, Double> distances = null;
	private StringBuilder text;
	private JLabel textLabel;
	private Map<String, List<GPSPoint>> gpsTracks;
	private StatTM tableModel;

	public StatsPanel() {
		Settings.getInstance().getPropertyChangeSupport().addPropertyChangeListener(this);
		tableModel = new StatTM();
		JTable table = new JTable(tableModel);
		table.setShowGrid(true);
		add(table);
	}

	private class StatTM extends DefaultTableModel{
		int colCount = 3;
		
		List<String> names = new ArrayList<String>();
		
		List<Double> distances = new ArrayList<Double>();
		
		Map<String, Double> distancesInSelection = new HashMap<String, Double>();
		
		NumberFormatter nf = new NumberFormatter(new DecimalFormat("0.000"));
		
		@Override
		public int getRowCount() {
			if (names == null) return 0;
			return names.size();
		}
		
		@Override
		public int getColumnCount() {
			return colCount;
		}
		
		@Override
		public String getColumnName(int column) {
			return (column == 0 ? "Name" : "Distance");
		}
		
		@Override
		public Object getValueAt(int row, int column) {
			if (column == 0) {
				return names.get(row);
			} else if (column == 1) {
				try {
					return nf.valueToString(distances.get(row) / 1000) + " km";
				} catch (ParseException e) {
					e.printStackTrace();
				}
			} else if (column == 2) {
				String name = names.get(row);
				if (distancesInSelection.containsKey(name) == false) {
					return "-";
				}
				try {
					return nf.valueToString(distancesInSelection.get(name) / 1000) + " km";
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
//			} else if (column == 3){
//				return new Boolean(true);
//			}
			throw new IllegalAccessError("row " + row + " col " + column);
		}
		
		
		public void addDistance(String name, double dist) {
			names.add(name);
			distances.add(dist);
			fireTableDataChanged();
		}
		
		public void addDistanceInSelection(String name, double dist) {
			distancesInSelection.put(name, dist);
			fireTableDataChanged();
		}

		public void removeAll() {
			names = new ArrayList<String>();
			distances = new ArrayList<Double>();
		}
		
		
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if (evt.getPropertyName().equals(Settings.getInstance().LOADED_GPS_FILES)) {
			tableModel.removeAll();
			gpsTracks = (Map<String, List<GPSPoint>>) evt.getNewValue();
			for (Map.Entry<String, List<GPSPoint>> entry : gpsTracks.entrySet()) {
				tableModel.addDistance(entry.getKey(), Distance.getTotalDist(entry.getValue()));
			}
		} else if (evt.getPropertyName().equals(Settings.getInstance().RANGE_CHANGE)) {
			long[] startStop = (long[]) evt.getNewValue();
			for (Map.Entry<String, List<GPSPoint>> entry : gpsTracks.entrySet()) {
				tableModel.addDistanceInSelection(entry.getKey(), Distance.getTotalDist(entry.getValue(), startStop));
			}
		}
	}
	
	public static void main(String[] args) {
		JFrame f = new JFrame();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		StatsPanel statPanel = new StatsPanel();
		f.getContentPane().add(statPanel);
		f.setSize(300, 400);
		f.setVisible(true);
		StatTM statTM = statPanel.tableModel;
		for (int i = 0; i < 10; i++) {
			statTM.addDistance("Hallo " + i, i * 1000);
		}
		
	}
}
